package org.flexharmony.eclipse.properties;

import static org.flexharmony.eclipse.utils.CommonMethods.*;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.flexharmony.eclipse.HarmonyPlugin;

abstract public class AbstractHarmonyPropertiesPanel extends Composite
{
  protected IProject project;
  
  public AbstractHarmonyPropertiesPanel(IProject project, Composite parent)
  {
    this(project, parent, SWT.NONE);
  }

  public AbstractHarmonyPropertiesPanel(IProject project, Composite parent, int style)
  {
    super(parent, style);
    this.project = project;
  }

  protected void setProperty(String name, String value) throws CoreException
  {
    if (project != null)
    {
      project.setPersistentProperty(new QualifiedName(HarmonyPlugin.PLUGIN_ID, name), value);
    }
    else
    {
      setPreference(name, value);
    }
  }
  
  protected String getPropertyString(String name) throws CoreException
  {
    if (project != null)
    {
      QualifiedName qname = new QualifiedName(HarmonyPlugin.PLUGIN_ID, name);
      String value = project.getPersistentProperty(qname);
      if (value != null)
      {
        return value;
      }
      else
      {
        return getPreference(name);
      }
    }
    else
    {
      return getPreference(name);
    }
  }

  protected String getPreference(String name)
  {
    return HarmonyPlugin.getInstance().getPluginPreferences().getString(name);
  }
  
  protected void setPreference(String name, String value)
  {
    HarmonyPlugin.getInstance().getPluginPreferences().setValue(name, value);
  }
}
